package project.ydlChat;

/**
 * @author 杜伟毅
 * @version 1.0
 * @since 2024/09/26
 * 动力chat
 */

import project.ydlChat.constant.MessageType;
import project.ydlChat.util.MsgUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Optional;

/**
 * 功能：1.给服务器发消息  2.给好友发消息  3.群发
 * 1.用什么类型去发消息
 */
public class Client {


    public static void main(String[] args) throws IOException {
        Socket socket = new Socket();
        //连接服务器
        socket.connect(new InetSocketAddress(8888));

        //发消息
        OutputStream outputStream = socket.getOutputStream();
        InputStream inputStream = socket.getInputStream();

        //先登录
        Message message = new Message(MessageType.Login, "zhangsi", "123");
        MsgUtils.writeMsg(outputStream, message);

        outputStream.flush();

        //接收来自服务端的消息
        Optional<Message> msg = MsgUtils.readMsg(inputStream);
        msg.ifPresent(System.out::println);



        outputStream.close();
        socket.close();
    }
}
